package studio.raptor.dispatcher.server.controller;

import java.util.Map;
import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import studio.raptor.dispatcher.server.listeners.ConsumerEventListener;
import studio.raptor.dispatcher.server.task.ConsumerTask;
import studio.raptor.dispatcher.server.task.DispatcherTask;

/**
 * @author jack
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/task")
public class TaskController {

  private static final Logger log = LoggerFactory.getLogger(TaskController.class);

  @Autowired
  ConsumerEventListener consumerEventListener;

  /**
   * 停止所有的消费和分发任务
   */
  @GetMapping(value = "/suspend/all")
  public void stopAllTask() {
    log.info("suspend  dispatcher server all task");
    Map<String, ConsumerTask> consumerTaskMap = consumerEventListener.getConsumerTaskMap();
    Map<String, DispatcherTask> dispatcherTaskMap = consumerEventListener.getDispatcherTaskMap();
    for (Entry<String, ConsumerTask> entry : consumerTaskMap.entrySet()) {
      ConsumerTask consumerTask = entry.getValue();
      DispatcherTask dispatcherTask = dispatcherTaskMap.get(entry.getKey());
      consumerTask.setStart(false);
      dispatcherTask.setStart(false);
    }
  }


  @GetMapping(value = "/suspend")
  public void stopAllTask(@RequestParam(value = "code") String code,
      @RequestParam(value = "kindId") Integer kindId) {
    log.info("suspend  dispatcher server all task");
    Map<String, ConsumerTask> consumerTaskMap = consumerEventListener.getConsumerTaskMap();
    Map<String, DispatcherTask> dispatcherTaskMap = consumerEventListener.getDispatcherTaskMap();
    String key = code + "-" + kindId;
    ConsumerTask consumerTask = consumerTaskMap.get(key);
    DispatcherTask dispatcherTask = dispatcherTaskMap.get(key);
    consumerTask.setStart(false);
    dispatcherTask.setStart(false);
  }


}
